package com.example.google_map_test;

import java.util.List;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class Map_ADD extends MapActivity {
	private MapView mapView;
	private MapController mapController;
	private MyLocationOverlay mylayer;

	public int id_this;
	List<Overlay> mapOverlays;
	Drawable drawable;
	double Latitude;
	double Longitude;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_add);

		// findControl(); 原本是在下面

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(false);
		mapController = mapView.getController();
		mapController.setZoom(16);
		// 定位點
		List<Overlay> overlays = mapView.getOverlays();
		mylayer = new MyLocationOverlay(this, mapView);
		// 顯示羅盤
		mylayer.enableCompass();
		// 啟動更新(如果坐標有變動會跟著移動)
		mylayer.enableMyLocation();
		mylayer.runOnFirstFix(new Runnable() {
			public void run() {
				mapController.animateTo(mylayer.getMyLocation());
			}
		});
		overlays.add(mylayer);

		mapOverlay myOverlay = new mapOverlay();
		List<Overlay> overlays2 = mapView.getOverlays();
		overlays2.add(myOverlay);

	}

	public class mapOverlay extends com.google.android.maps.Overlay {
		public boolean onTouchEvent(MotionEvent event, MapView mapview) {

			if (event.getEventTime() - event.getDownTime() > 1000) {

				GeoPoint p = mapView.getProjection().fromPixels(
						(int) event.getX(), (int) event.getY());

				Latitude = p.getLatitudeE6() / 1E6;
				Longitude = p.getLongitudeE6() / 1E6;

				Toast.makeText(
						getBaseContext(),
						p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6()
								/ 1E6, Toast.LENGTH_SHORT).show();
				dialog();
			}
			return false;
		}

	}

	protected void dialog() {
		AlertDialog.Builder builder = new Builder(Map_ADD.this);
		builder.setMessage("在此地加入一筆資料?");
		builder.setTitle("訊息");
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				Intent ins = new Intent(Map_ADD.this, ADD.class);
				Bundle Data = new Bundle();
				// 寫入資料到 Bundle 中
				Data.putDouble("Latitude", Latitude);

				Data.putDouble("Longitude", Longitude);

				// 將 Bundle 指定到 Intent
				ins.putExtras(Data);
				startActivity(ins);
				// Map_ADD.this.finish();
			}

		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create().show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, 1, 1, "地圖").setIcon(R.drawable.menu_map);
		menu.add(0, 2, 2, "增加").setIcon(R.drawable.menu_add);
		menu.add(0, 3, 3, "檢視").setIcon(R.drawable.menu_view);
		menu.add(0, 4, 4, "訊息").setIcon(R.drawable.menu_help);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			Intent ins = new Intent(Map_ADD.this, MainActivity.class);
			startActivity(ins);
			Map_ADD.this.finish();
			break;
		case 2:
			Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show();
			break;
		case 3:
			Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
			break;
		case 4:
			Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
			break;

		}

		return false;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
